package com.kl.xmlapitest;

import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class XmlAPITest {

    Element root;

    @Before
    public void init() throws ParserConfigurationException, IOException, SAXException {
        // 定义工厂API 使应用程序能够从XML文档获取生成DOM对象树的解析器
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // 获取此类的实例之后，将可以从各种输入源解析XML
        DocumentBuilder builder = factory.newDocumentBuilder();
        // builder.parse(this.getClass().getResourceAsStream("/" + filePath));
        // Document接口表示整个HTML或XML文档，从概念上讲，它是文档树的根，并提供对文档数据的基本访问
        Document document = builder.parse("src/main/java/com/kl/xmlapitest/test2.xml");
        // 获取根节点
        root = document.getDocumentElement();
    }

    /**
     * test.xml获取具体结点里面的值
     */
    @Test
    public void test1() {
        NodeList toList = root.getElementsByTagName("to");
        System.out.println(toList.item(0).getTextContent());
        NodeList fromList = root.getElementsByTagName("from");
        System.out.println(fromList.item(0).getTextContent());
    }

    /**
     * student.xml用element的方式解析
     */
    @Test
    public void test2() {
        NodeList list = root.getElementsByTagName("student");
        int len = list.getLength();
        System.out.println("student标签数量 = " + len);
        for (int i = 0; i < len; i++) {
            Element element = (Element) list.item(i);
            NodeList childNodes = element.getChildNodes();
            int childLen = childNodes.getLength();
            System.out.println("student子标签数量 = " + childLen);
            for (int j = 0; j < childLen; j++) {
                if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                    // 获取节点的名称
                    System.out.print(childNodes.item(j).getNodeName() + " : ");
                    // 两种获取节点存储的值的方式
                    // getTextContent   实际获取的是所有子节点的值
                    String content = childNodes.item(j).getTextContent();
                    // getNodeValue是获取当前节点的值
                    String nodeValue = childNodes.item(j).getFirstChild().getNodeValue();
                    System.out.println(content + "--->" + content.equals(nodeValue));
                }
            }
        }
        System.out.println(root.getNodeName());
    }

    /**
     * student.xml用node的方式解析
     */
    @Test
    public void test3() {
        NodeList list = root.getElementsByTagName("student");
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                    System.out.print(childNodes.item(j).getNodeName() + ":");
                    System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
                }
            }
        }
    }

    /**
     * test2.xml多重嵌套解析
     */
    @Test
    public void test4() {
        // 可以获取根结点下任意一个结点的值
        NodeList text = root.getElementsByTagName("Value");
        System.out.println(text.item(0).getFirstChild().getNodeValue());
        System.out.println(text.item(0).getTextContent());
    }

    /**
     * 解析test.xml数据
     */
    @Test
    public void test5() throws Exception {
        // 定义工厂API 使应用程序能够从XML文档获取生成DOM对象树的解析器
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // 获取此类的实例之后，将可以从各种输入源解析XML
        DocumentBuilder builder = factory.newDocumentBuilder();
        // builder.parse(this.getClass().getResourceAsStream("/" + filePath));
        // Document接口表示整个HTML或XML文档，从概念上讲，它是文档树的根，并提供对文档数据的基本访问
        Document document = builder.parse("src/main/java/com/kl/xmlapitest/test.xml");
        // 获取根节点
        Element root = document.getDocumentElement();
        // 根结点的名字
        System.out.println(root.getNodeName());
        // 获取具体结点里面的值
        NodeList toList = root.getElementsByTagName("to");
        System.out.println(toList.item(0).getTextContent());
        NodeList fromList = root.getElementsByTagName("from");
        System.out.println(fromList.item(0).getTextContent());
        // 解析为数组
        NodeList idsList = root.getElementsByTagName("ids");
        String ids = idsList.item(0).getTextContent();
        System.out.println(ids);
        List<Integer> list = new ArrayList<>();
        String[] split = ids.split(",");
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        System.out.println(list);
    }
}
